﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using YaYun.Models;

namespace YaYun.Controllers
{
    public class LoginIndexController : Controller
    {
        // GET: LoginIndex
        public ActionResult Login()
        {
            return View();
        }

        [HttpPost]     
    public ActionResult Login(admin admin)
        {
            if (ModelState.IsValid)
            {
                using (var db = new YayunEntities3())
                {
                    var user = db.admin.FirstOrDefault(n => n.username == admin.username || n.adminPassword == admin.adminPassword || n.mobile == admin.mobile);

                    if (user != null && user.adminPassword == admin.adminPassword)
                    {
                        string emailId = user.email; // 假设需要匹配的邮箱ID为example@example.com
                        var e = db.admin.FirstOrDefault(n => n.email.Contains(emailId));
                        if (admin != null)
                        {
                            Session["EmployeeName"] = e.username;
                            // 在这里可以使用Session["EmployeeName"]进行后续操作
                            Session["touxiang"] = e.adminPic;
                        }

                        // 登录成功，跳转到主页
                        return RedirectToAction("Index", "Home");
                    }
                    else
                    {
                        // 登录失败，返回错误信息
                        ModelState.AddModelError("", "账号或者密码错误！");
                    }
                }
            }
            // 返回登录页面，显示错误信息
            return View(admin);
        }

      
        public ActionResult Logout()
        {

            Session.Clear(); // 清除当前用户的会话
            return Content("<script>alert('退出登录！');window.location='/LoginIndex/Login'</script>");

        }
    }
}